package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 14467
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param dto
     * @return
     */
    @Override
    public void add(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        // 获取当前登录人id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        shoppingCart.setDishId(dto.getDishId());
        shoppingCart.setSetmealId(dto.getSetmealId());
        shoppingCart.setDishFlavor(dto.getDishFlavor());
        // 判断当前添加的数据是第一条还是已经添加过的
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        // 添加过的则数量加1
        if (shoppingCartList != null && shoppingCartList.size() == 1) {
            // 说明有数据，在加1
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            // 修改数据
            shoppingCartMapper.update(shoppingCart);
        } else {
            // 没有数据则添加上去
            if (dto.getDishId() != null) {
                // 说明添加的是菜品
                DishVO dish = dishMapper.getDishById(dto.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                // 添加的是菜品
                SetmealVO setmealVO = setmealMapper.selectById(dto.getSetmealId());
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setAmount(setmealVO.getPrice());
            }
            // 设置初始数量和值
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = new ShoppingCart();
        // 获取当前登录人id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @Override
    public void clean() {
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }

    /**
     * 删除购物车中一个商品
     * @param dto
     * @return
     */
    @Override
    public void sub(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        // 获取当前登录人id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        shoppingCart.setDishId(dto.getDishId());
        shoppingCart.setSetmealId(dto.getSetmealId());
        // 判断当前添加的数据是第一条还是已经添加过的
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        if(shoppingCartList != null && shoppingCartList.size() > 0){
            shoppingCart = shoppingCartList.get(0);
            // 获取数据的number
            if(shoppingCart.getNumber() > 1){
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                // 修改数据
                shoppingCartMapper.update(shoppingCart);
            }else {
                // 删除这条数据
                shoppingCartMapper.deleteById(currentId,shoppingCart.getId());
            }
        }
    }
}